<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="sortId">
        <el-cascader :options="sortList"
                     :props="{label: 'name', value: 'id', children: 'children',emitPath:false}"
                     v-model="queryParams.sortId"
                     clearable filterable
                     placeholder="请选择分类" @change="handleQuery" style="width: 100%"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kaifa:quest:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kaifa:quest:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kaifa:quest:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kaifa:quest:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="发起人" align="center" prop="initiatorName"/>
      <el-table-column label="接收人" align="center" prop="receiverName"/>
      <el-table-column label="订单状态" align="center" prop="publishStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.publishStatus === 0" type="info">草稿</el-tag>
          <el-tag v-else-if="scope.row.publishStatus === 1" type="warning">已发布</el-tag>
          <el-tag v-else-if="scope.row.publishStatus === 2">进行中</el-tag>
          <el-tag v-else-if="scope.row.publishStatus === 3" type="success">已完成</el-tag>
          <el-tag v-else-if="scope.row.publishStatus === 4" type="danger">售后中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="内容" align="center" prop="content"/>
      <el-table-column label="分类" align="center" prop="sortName"/>
      <el-table-column label="封面" align="center" prop="cover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="详细地址" align="center" prop="detailsAddress"/>
      <el-table-column label="经纬度" align="center" prop="coordinate"/>
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180" :formatter="formatDateTime"/>
      <el-table-column label="截止时间" align="center" prop="cutoffTime" width="180" :formatter="formatDateTime"/>
      <el-table-column label="报酬" align="center" prop="price"/>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kaifa:quest:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kaifa:quest:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发起人" prop="initiatorId">
          <el-select v-model="form.initiatorId" clearable filterable placeholder="请选择发起人">
            <el-option
              v-for="dict in userList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="接收人" prop="receiverId">
          <el-select v-model="form.receiverId" clearable filterable placeholder="请选择接收人">
            <el-option
              v-for="dict in userList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="订单状态" prop="publishStatus">
          <el-select v-model="form.publishStatus" placeholder="请选择订单状态">
            <el-option
              v-for="dict in orderStatusOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <image-upload v-model="form.cover"/>
        </el-form-item>
        <el-form-item label="分类id" prop="sortId">
          <el-cascader :options="sortList"
                       :props="{label: 'name', value: 'id', children: 'children',emitPath:false}"
                       v-model="form.sortId"
                       placeholder="请选择分类"
          />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailsAddress">
          <el-input v-model="form.detailsAddress" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="经纬度" prop="coordinate">
          <el-input v-model="form.coordinate" placeholder="请输入经纬度"/>
        </el-form-item>

        <el-form-item label="发布时间" prop="publishTime">
          <!--精确至时分秒-->
          <el-date-picker clearable
                          v-model="form.publishTime"
                          type="datetime"
                          placeholder="请选择发布时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止时间" prop="cutoffTime">
          <el-date-picker clearable
                          v-model="form.cutoffTime"
                          type="datetime"
                          placeholder="请选择截止时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报酬" prop="price">
          <el-input v-model="form.price" placeholder="请输入报酬"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listQuest, getQuest, delQuest, addQuest, updateQuest } from '@/api/kaifa/quest'
export default {
  name: 'Quest', data() {
    return {
      loading: true,
      orderStatusOptions: [{ label: '草稿', value: 0 }, { label: '已发布', value: 1 }, {
        label: '进行中',
        value: 2
      }, { label: '已完成', value: 3 }, {
        label: '售后中',
        value: 4
      }],
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      questList: [],
      userList: [],
      sortList: [],
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        sortId: null,
        cover: null
      }, form: {}, rules: { initiatorId: [{ required: true, message: '发起人id不能为空', trigger: 'blur' }] }
    }
  }, created() {
    this.getList()
  }, methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true
      listQuest(this.queryParams).then(response => {
        this.questList = response.list.rows
        this.total = response.list.total
        this.sortList = this.handleTree(response.sortList, 'id', 'parentId')
        this.userList = response.userList
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: null,
        initiatorId: null,
        receiverId: null,
        publishStatus: null,
        title: null,
        content: null,
        sortId: null,
        cover: null,
        orderStatus: null,
        address: null,
        detailsAddress: null,
        coordinate: null,
        publishTime: null,
        cutoffTime: null,
        price: null,
        createTime: null,
        updateTime: null,
        isDelete: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }, handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加任务'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getQuest(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改任务'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuest(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addQuest(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除任务编号为"' + ids + '"的数据项？').then(function() {
        return delQuest(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    formatDateTime(row, column, cellValue, index) {
      if (!cellValue) return '';
      const date = new Date(cellValue);
      const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
      return date.toLocaleString('zh-CN', options);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kaifa/quest/export', { ...this.queryParams }, `quest_${new Date().getTime()}.xlsx`)
    }
  }
} </script>
